package cn.kgm.makeGodV2;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Version 1.8
 * @Author: Kappi
 * @Date: 2025-9-26 11:13
 * @Description: cn.kgm.makeGodV2.CompositeCondition
 */
public class CompositeCondition implements ICondition {

    private LogicalOperator operator;
    private List<ICondition> subConditions;

    @Override
    public String getConditionType() {
        return null;
    }

    @Override
    public boolean evaluate(IEvaluationContext context) {
        switch (operator) {
            case AND:
                return subConditions.stream().allMatch(cond -> cond.evaluate(context));
            case OR:
                return subConditions.stream().anyMatch(cond -> cond.evaluate(context));
            case NOT:
                return subConditions.size() == 1 && !subConditions.get(0).evaluate(context);
            case XOR:
                return subConditions.stream().filter(cond -> cond.evaluate(context)).count() == 1;
            default:
                return false;
        }
    }

    @Override
    public String getDescription() {
        String conditionDescs = subConditions.stream()
                .map(ICondition::getDescription)
                .collect(Collectors.joining(" " + operator.name() + " "));
        return "(" + conditionDescs + ")";
    }
}
